package com.soft.mainsite.component.beetl.tag.cms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import com.soft.mainsite.module.viewshow.entity.ViewShow;
import com.soft.mainsite.module.viewshow.service.ViewShowService;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 根据栏目动态真是新增输入框选项
 *
 * @author liupengtao
 * @date 2019/05/27
 */
@Slf4j
@Service
@Scope("prototype")
public class ViewShowTag extends GeneralVarTagBinding {

    @Autowired
    private ViewShowService viewShowService;


    @Override
    public void render() {
        String viewId = Convert.toStr(this.getAttributeValue("viewId"));
        Integer categoryId = Convert.toInt(this.getAttributeValue("categoryId"));
        wrapRender(viewId, categoryId);
    }

    private void wrapRender(String viewId, Integer categoryId) {
        ViewShow viewShow = viewShowService.selectShowIds(categoryId);
        Map result = MapUtil.newHashMap();

        if (viewShow != null) {
            String[] ids = viewShow.getShowIds().split(",");
            if (ArrayUtil.contains(ids, viewId)){
                result.put("isShow", true);
            }
        }

        this.binds(result);
        this.doBodyRender();
    }
}
